//
//  HomePendingCell.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/7/27.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "HomePendingCell.h"
#import "HomeMenuView.h"
#import "MyShipmentMainVC.h"
#import "MyCollectMainVC.h"

@interface HomePendingCell ()

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIImageView *logoImgView;
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *countLab;
@property (nonatomic, strong) UILabel *descLab;
@property (nonatomic, strong) UIScrollView *scrollView;

@end

@implementation HomePendingCell

+ (instancetype)HomePendingCellWithTableView:(UITableView *)tableView {
    static NSString *cellID = @"HomePendingCell";
    HomePendingCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[HomePendingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.bgView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, ScreenW - 20, 151)];
        self.bgView.backgroundColor = HexColor(0xffffff);
        self.bgView.layer.masksToBounds = YES;
        self.bgView.layer.cornerRadius = 6;
        [self.contentView addSubview:self.bgView];
        
        self.logoImgView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 17, 18, 18)];
        self.logoImgView.image = [UIImage imageNamed:@"icon_pending"];
        [self.bgView addSubview:self.logoImgView];
        
        self.titleLab = [[UILabel alloc] initWithFrame:CGRectMake(self.logoImgView.nim_right + 8, 17.5, [FactoryMethod sizeWithText:@"待处理" font:BoldFontOfSize(16) maxSize:CGSizeMake(0, 0)].width, 17)];
        self.titleLab.textColor = HexColor(0x2B2C44);
        self.titleLab.font = BoldFontOfSize(16);
        self.titleLab.text = @"待处理";
        [self.bgView addSubview:self.titleLab];
        
        self.countLab = [UILabel new];
        self.countLab.textColor = HexColor(0x9C9DB3);
        self.countLab.font = [UIFont systemFontOfSize:12];
        [self.bgView addSubview:self.countLab];
        
        self.descLab = [[UILabel alloc] initWithFrame:CGRectMake(self.bgView.nim_width - 10 - 140, 19.5, 140, 13)];
        self.descLab.textColor = HexColor(0x9C9DB3);
        self.descLab.textAlignment = NSTextAlignmentRight;
        self.descLab.font = [UIFont systemFontOfSize:12];
        self.descLab.text = @"等待您处理的事项";
        [self.bgView addSubview:self.descLab];
        
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(15, self.logoImgView.nim_bottom + 17, self.bgView.nim_width - 30, 84)];
        self.scrollView.backgroundColor = [UIColor whiteColor];
        self.scrollView.bounces = NO;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        [self.bgView addSubview:self.scrollView];
        
        self.height = self.bgView.nim_bottom;
    }
    return self;
}

-(void)setPendingArray:(NSArray<HomeMenuModel *> *)pendingArray {
    _pendingArray = pendingArray;
    CGFloat margin = (self.scrollView.nim_width - 84 * 3) * 0.5;
//    long i = pendingArray.count / 2 + ((pendingArray.count % 2) > 0 ? 1 : 0);
    self.scrollView.contentSize = CGSizeMake(self.bgView.nim_width - 30, 84);
    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    __weak_self;
    [pendingArray enumerateObjectsUsingBlock:^(HomeMenuModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        HomeMenuView *view = [[HomeMenuView alloc] initWithFrame:CGRectMake(84 * idx + margin * idx, 0, 84, 84)];
        view.menuModel = obj;
        view.clickBlock = ^{
            if (idx == 0 || idx == 1) {
                //受理、回单确认
                MyShipmentMainVC *vc = [MyShipmentMainVC new];
                vc.hidesBottomBarWhenPushed = YES;
                [[FactoryMethod getCurrentVC].rt_navigationController pushViewController:vc animated:YES];
            }else {
                //运单签收
                MyCollectMainVC *vc = [MyCollectMainVC new];
                vc.hidesBottomBarWhenPushed = YES;
                [[FactoryMethod getCurrentVC].rt_navigationController pushViewController:vc animated:YES];
            }
        };
        [weakself.scrollView addSubview:view];
    }];
}

-(void)setTotal_accept:(NSInteger)total_accept {
    _total_accept = total_accept;
    self.countLab.text = [NSString stringWithFormat:@"(%ld)",(long)total_accept];
    [self.countLab sizeToFit];
    self.countLab.nim_left = self.titleLab.nim_right + 5;
    self.countLab.nim_centerY = self.titleLab.nim_centerY;
}

//-(void)setPendingArray:(NSArray<HomeMenuModel *> *)pendingArray {
//    _pendingArray = pendingArray;
//    CGFloat margin = (self.scrollView.nim_width - 84 * 3) * 0.5;
//    long i = pendingArray.count / 2 + ((pendingArray.count % 2) > 0 ? 1 : 0);
//    self.scrollView.contentSize = CGSizeMake(84 * i + margin * (i - 1), 84 * 2);
//    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
//    __weak_self;
//    [pendingArray enumerateObjectsUsingBlock:^(HomeMenuModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        HomeMenuView *view = [[HomeMenuView alloc] initWithFrame:CGRectMake(84 * (idx / 2) + margin * (idx > 1 ? (idx / 2) : 1 - 1), idx % 2 > 0 ? 84 : 0, 84, 84)];
//        view.menuModel = obj;
//        [weakself.scrollView addSubview:view];
//    }];
//}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
